
pageSearch();

//显示表
function buildProblemList(result, tableId, bodyId){
    var oProblemList = document.querySelector(tableId);
    var oTBody =oProblemList.querySelector(bodyId);
    //创建
    var str ='';
    //创建
    var fragment =document.createDocumentFragment();
    for(var problem of result.data){
      //
      var percent;
      if(problem.acCount == 0 && problem.visitCount == 0){
          percent = 0;
      }else{
          percent = problem.acCount/problem.visitCount*100;
      }
      percent = percent.toFixed(1);
      var state = "&#xe64d;";
      if(problem.acState == 1){
          state = '&#xe62b;';
      }
      str += `
      <tr>
        <td class="iconfont">${state}</td>
        <td><a href="coding.html?id=${problem.id}">${problem.id}. ${problem.title}</a></td>
        <td>${percent}%</td>
        <td>${problem.level}</td>
      </tr>
      `
    }
    oTBody.innerHTML = str;
  }

// 获取 url querystring 的值 
// function getQueryString(name) {
//     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
//     var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
//     var context = "";
//     if (r != null)
//     context = r[2];
//     reg = null;
//     r = null;
//     return context == null || context == "" || context == "undefined" ? (name == 'page' ? 1 : (name == 'pageSize' ? 10 : "")) : context;
// }


function search(){
    var input = document.querySelector('#searchQuestion');
    var question = input.value;
    var input = document.querySelector('#on');
    var classify = input.innerHTML;
    getData(1, question, classify);

}


function pageSearch() {
  var input = document.querySelector('#on');
  var question = input.innerHTML;
  var classify = question;
  if(question == '总题库'){
      question = '';
  }else if(question == '简单题'){
      question = '简单';
  }else if(question == '中等题'){
      question = '中等';
  }else if(question == '困难题'){
      question = '困难';
  }
  // 进入页面初始化请求第一页数据
  getData(1, question, classify);
}

function adminSearch() {
  var input = document.querySelector('#on');
  var question = input.innerHTML;
  getData(1, question, question);


}

// 使用ajax函数请求数据并渲染到页面
function getData(page, question, classify) {
  var url = 'search';
  if(question == '用户管理'){
    url = 'userSearch';
  }
  jQuery.ajax({
    url: url,
    type: "post",
    contentType:'application/json',
    data: JSON.stringify(
      {          
        page: page,
        pageSize: 10,
        question:question,
        token:localStorage.getItem("token")
    }),
    dataType:'json',
    headers: {
      'Authorization': localStorage.getItem("token")
    },
    success: function (result) {
      if(result != null && result.code == 200){
        if(classify == '总题库'){
          buildProblemList(result, '#classify01', '#classify01 tbody');
        }else if(classify == '简单题'){
          buildProblemList(result, '#classify02', '#classify02 tbody');
        }else if(classify == '中等题'){
          buildProblemList(result, '#classify03', '#classify03 tbody');
        }else if(classify == '困难题'){
          buildProblemList(result, '#classify04', '#classify04 tbody');
        }else if(classify == '已完成'){
          buildProblemList(result, '#classify05', '#classify05 tbody');
        }else if(classify == '题目管理'){
          buildAdmin(result, '#problem', '#problem tbody');
        }else if(classify == '用户管理'){
          buildAdmin(result, '#user', '#user tbody');
        }
        var paging = document.querySelector('#pagination');
        var dataTotal;
        if(result.data != ""){
          if(classify == '题目管理' || classify == '用户管理'){
            dataTotal = result.data.count;
          }else{
            dataTotal = result.data[0].count;
          }
        }else{
          dataTotal = 0;
        }
        new myPagination({
          id: 'pagination',
          curPage: page, //初始页码
          pageTotal: Math.ceil(dataTotal/10), //总页数
          pageAmount: 10, //每页多少条
          dataTotal: dataTotal, //总共多少条数据
          pageSize: 5, //可选,分页个数
          showPageTotalFlag: true, //是否显示数据统计
          showSkipInputFlag: true, //是否支持跳转
          getPage: function (page) {
            // 点击分页按钮请求数据
            getData(page, question, classify);
          }
        })
      }else{
        alert("获取数据失败！");
      }
    }
  })
}